﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using Dexter.Dependency;
using Dexter.Domain.Model;
using Dexter.Logger.Contracts;
using Dexter.PluginManager;
using Dexter.Search;
using Dexter.Services;
using Dexter.Web.Mvc.Controllers.Backoffice.Extensions;
using Dexter.Web.Mvc.Extensions;
using Dexter.Web.Mvc.Filters;
using Dexter.Web.Mvc.ViewModel.Backoffice.Binders;
using Dexter.Web.Mvc.ViewModel.Backoffice.ViewsData.SettingsController;
using Dexter.Web.Utils;

namespace Dexter.Web.Mvc.Controllers.Backoffice {
	[Authorize ( Roles = "Administrator" )]
	[DexterCallFilter ( false )]
	public class SettingsController : BackOfficeControllerBase {
		readonly IIndexService indexService;
		readonly IPluginService pluginService;
		readonly IPluginManager pluginManager;

		public SettingsController ( IConfigurationService configurationService , IWebUtils webUtils , ILogger logger , IUrlBuilder urlBuilder , IContactService contactService , IIndexService indexService, IPluginService pluginService, IPluginManager pluginManager) : base ( configurationService , webUtils , logger , urlBuilder , contactService ) {
			this.indexService = indexService;
			this.pluginService = pluginService;
			this.pluginManager = pluginManager;
		}

		[AcceptVerbs ( HttpVerbs.Get )]
		public ActionResult Site() {
			SiteViewData data = GetConfigurationData ( );
			return View ( data );
		}

		[ValidateInput(false)]
		[AcceptVerbs ( HttpVerbs.Post )]
		[ValidateAntiForgeryToken]
		public ActionResult Site ( ConfigurationBinder configurationBinder ) {
			if (!ModelState.IsValid) {
				var data = GetConfigurationData ( );
				data.ConfigurationBinder = configurationBinder;

				return View ( data );
			}

			var currentConfiguration = base.Configuration;

			var newConfiguration = configurationBinder.ToSiteConfiguration(currentConfiguration);

			ConfigurationService.SaveConfiguration ( newConfiguration );

			return RedirectToAction ( "Positive", "Message", new { Area = "Admin", id = "ConfigurationSaved" } );
		}

		[AcceptVerbs ( HttpVerbs.Get )]
		public ActionResult RebuildSearchIndex() {
			indexService.RebuildIndex (  );
			
			return this.ReturnFeedback ( MessageType.Positive, "RebuildIndexStarted", true );
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult PluginSettings() {
			PluginsViewData data = GetPluginsData();

			return View(data);
		}

		PluginsViewData GetPluginsData() {
			var data = base.GetViewData(new PluginsViewData(base.WebUtils, base.Configuration, base.UrlBuilder));

			data.Plugins = pluginService.GetAllPluginsDto();

			return data;
		}

		SiteViewData GetConfigurationData ( ) {
			var data = base.GetViewData ( new SiteViewData ( base.WebUtils, base.Configuration, base.UrlBuilder ) );

			data.ConfigurationBinder = this.Configuration.ToConfigurationBinder ( );

			DirectoryInfo dInfo = new DirectoryInfo ( Server.MapPath ( "/Themes/" ) );
			data.Themes = dInfo.GetDirectories ( ).Select ( x => x.Name ).ToArray ( );
			data.TimesZone = TimeZoneInfo.GetSystemTimeZones ( );
			data.Languages = new[] {"Danish" , "Dutch" , "English" , "Finnish" , "French" , "German2" , "German" , "Italian" , "Kp" , "Lovins" , "Norwegian" , "Porter" , "Portuguese" , "Russian" , "Spanish" , "Swedish"};
			data.CultureInfos = CultureInfo.GetCultures ( CultureTypes.AllCultures );

			data.ModerationType = new Dictionary<int, string>
			                      {
			                      	{(int) ModerationType.First, "Pre-Moderation"}, 
			                      	{(int) ModerationType.Post, "Post-Moderation"}
			                      };

			data.RecaptchaThemes = new[] { "red", "default", "white", "blackglass", "clean", "custom" };

			return data;
		}
	}
}